package com.wenjie.redHat.controller;


import com.wenjie.redHat.commom.R;
import com.wenjie.redHat.domain.entity.ProductEntity;
import com.wenjie.redHat.service.ProductInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/productInfo")
@Api
public class ProductInfoController {
    @Autowired
    private ProductInfoService productInfoService;

    @ApiOperation("产品保存接口")
    @PostMapping("/save")
    public R saveProduct(@RequestBody ProductEntity productEntity){

        return R.success( productInfoService.save(productEntity));
    }
    @ApiOperation("产品更新接口")
    @PostMapping("/update")
    public R updateProduct(@RequestBody ProductEntity productEntity){
        return R.success(productInfoService.updateProduct(productEntity));
    }
    @ApiOperation("查找单个产品")
    @GetMapping("/get/{id}")
    public R getProductId(@PathVariable Integer id){
        return R.success(productInfoService.getById(id));
    }
    @ApiOperation("获取所有产品")
    @GetMapping("/getList")
    public R getList(){
        return R.success(productInfoService.list());
    }
    @ApiOperation("删除产品")
    @PostMapping("/delete/{id}")
    public R deleteId(@PathVariable Integer id){
        return R.success(productInfoService.removeById(id));

    }
    @ApiOperation("根据分类查找对应的产品")
    @GetMapping("/getByCid/{id}")
    public R getByCid(@PathVariable Integer id){
        ProductEntity productEntity =new ProductEntity();
        productEntity.setCid(id);
        productInfoService.getByCid(productEntity.getCid());
        if (productInfoService.getByCid(productEntity.getCid()) !=null){
            return R.success(productInfoService.getByCid(productEntity.getCid()));
        }
        else {
            return R.error("查找失败");
        }
    }
}
